package breeze.pixel.weather.service.app_widget;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.IBinder;

import brz.breeze.tool_utils.Blog;

public class AppWidgetUpdateService extends Service {
    public AppWidgetUpdateService() {
    }

    public static final String TAG = "AppWeatherUtils";

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("PixelID", "Pixel_OneName", NotificationManager.IMPORTANCE_HIGH);
            channel.enableLights(true);
            channel.setLightColor(Color.RED);
            channel.setShowBadge(true);
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            manager.createNotificationChannel(channel);
            startForeground(10001, new Notification.Builder(this, "PixelID")
                    .setChannelId("PixelID")
                    .build());
        }
        WeatherUtils.getWeatherNow(this);
        WeatherUtils.getWeather3D(this);
        WeatherUtils.getWeather7D(this);
        stopSelf();
        super.onCreate();
    }

    private static Intent service;
    public static void startService(Context context){
        service = new Intent(context, AppWidgetUpdateService.class);
        try{
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
                context.startForegroundService(service);
            }else{
                context.startService(service);
            }
        }catch (Exception e){
            Blog.e(e);
        }
    }

    public static void stopService(Context context){
        if (service!=null){
            context.stopService(service);
        }
    }
}